/*
  ABOUT-GUI JS > AJAX.
  @author: Luis M. Rodriguez <me at miguel dot weapps dot com>
*/


/**
 * The AJAXer var is the object handling all the ajax requests.
 */
var AJAXer = {
	/**
	 * XmlHttp request object.
	 */
	xmlHttp:{},
	
	/**
	 * The first function, to create the xmlHttp request object.
	 */
	start:function(){
		try{	// Firefox, Opera 8.0+, Safari
			this.xmlHttp=new XMLHttpRequest();
		}catch(e){
			// Internet Explorer
			try{
				this.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					this.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){
					BME.error("Your browser does not support AJAX! "+e);
					return false;
				}
			}
		}
		return true;
	},
	/**
	 * The main public method.
	 * @param str request: The request to the system.
	 * @param mix where: (str) Where to place the output, or (null) eval the output as js.
	 */
	request:function(request,where){
		if(where=="" || BME.setInnerHtml(where,request+"...")) {
			if(this.xmlHttp.readyState==4||this.xmlHttp.readyState==0){
				var url=BME.root+"request.php?sid="+Math.random()+"&request="+request;
				this.xmlHttp.onreadystatechange=function(){
					AJAXer.stateChanged(where);
				}
				this.xmlHttp.open("GET",url,true);
				this.xmlHttp.send(null);
			}else{
				setTimeout("AJAXer.request('"+request+"','"+where+"')",50);
			}
		}
	},
	/**
	 * The function checking if ajax task is ready.
	 */
	stateChanged:function(where){
		if(this.xmlHttp.readyState==4){
			if(this.xmlHttp.status == 200){
				if(where!=""){
					BME.setInnerHtml(where,this.xmlHttp.responseText);
					if(where!="foot_user") setTimeout('AJAXer.request("msg/user","foot_user")',100);
				}else{
					eval(this.xmlHttp.responseText);
				}
			}else{
				BME.error("AJAX failed with Error code: "+this.xmlHttp.status+" on request "+where);
			}
		}
	}
}
